package com.qf.shiro;

import com.qf.dao.MenuMapper;
import com.qf.dao.PermsettingMapper;
import com.qf.dao.UsermanagerMapper;
import com.qf.pojo.Menu;
import com.qf.pojo.Permsetting;
import com.qf.pojo.Usermanager;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 2019/11/26 0026
 * Author->DT
 * shiro1
 * 行到水穷处 坐看云起时
 * 模块：简单的自定义Realm
 */
//Shiro自带的IniRealm是从ini配置文件中读取用户的信息，当我们需要从数据库中读取时，就需要自定义Realm
@Component
public class CustomRealm extends AuthorizingRealm {
    public static void main(String[] args){
        System.out.println(new Md5Hash("123","test",1).toString());
    }
    @Autowired
    private UsermanagerMapper usermapper;
    @Autowired
    private PermsettingMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权开始");
        //得到认证成功后的用户信息
        Usermanager user = (Usermanager) principalCollection.getPrimaryPrincipal();
        //创建SimpleAuthorizationInfo对象
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //根据角色id查询角色
        Permsetting role = roleMapper.selectByPrimaryKey(user.getUserrole());
        //将角色信息添加到SimpleAuthorizationInfo中
        authorizationInfo.addRole(role.getPermname());
        //根据角色id查询出具体功能的请求权限菜单对象列表
        List<Menu> permissionList=menuMapper.selectPermissionByRoleId(role.getId());
        //创建存放请求权限码的列表
        ArrayList permCodeList = new ArrayList();
        for (Menu menu : permissionList) {
            permCodeList.add(menu.getPercode());
        }
        //将权限信息添加到SimpleAuthorizationInfo中
        authorizationInfo.addStringPermissions(permCodeList);

        return authorizationInfo;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("开始认证");
        //1.先获取提交的用户名,判断用户名是否存在
        String username = (String) authenticationToken.getPrincipal();
        //2.这里应该调取service，连接数据库进行查询，根据用户名得到对应的用户对象,判断对象是否存在
        Usermanager user = usermapper.login(username);
        //3.判断用户是否存在
        if (user==null){
            //用户名不存在
            return null;
        }
        System.out.println("执行customRealm结束");
        //4.将正确的数据库信息保存到认证器对象中, 参数(正确账户对应的用户对象,正确密码，当前realm的name)
        return new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()),"myReam");
    }
}
